//
//  EUExDemo.m
//  EUExDemo
//
//  Created by hui.li on 13-4-11.
//  Copyright (c) 2013年 hui.li. All rights reserved.
//

#import "EUExXG.h"
#import "EUtility.h"
#import "XGPush.h"
#import "XGSetting.h"
#define _IPHONE80_ 80000

@implementation EUExXG


-(id)initWithBrwView:(EBrowserView *)eInBrwView{
     self = [super initWithBrwView:eInBrwView];
     if (self) {
     }
     return self;
}

//初始化信鸽推送
-(void)InitXgPush:(NSMutableArray *)array{
     NSLog(@"in InitXgPush");
    
    
    double appid =[[array objectAtIndex:0] doubleValue];
    NSString *appkey =[array objectAtIndex:1];

    
    [XGPush startApp:appid appKey:appkey];
    [self InitXG];//调用通用的注册方法。
}


//初始化推送为用户绑定方式。
-(void)InitXgUserPush:(NSMutableArray *)array{
    NSLog(@"in InitXgUserPush");
    [XGPush setAccount:[array objectAtIndex:2]];//绑定注册者的账号。
    
     double appid =[[array objectAtIndex:0] doubleValue];
     NSString *appkey =[array objectAtIndex:1];
    [XGPush startApp:appid appKey:appkey];

     [self InitXG];//调用通用的注册方法。
     //[self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexXG.cbXgPush(0,0,'InitXgUserPush');"];
}
-(void)InitXG{
    
    //注销之后需要再次注册前的准备
    void (^successCallback)(void) = ^(void){
        //如果变成需要注册状态
        if(![XGPush isUnRegisterStatus])
        {
            //iOS8注册push方法
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
            
            float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
            if(sysVer < 8){
                [self registerPush];
            }
            else{
                [self registerPushForIOS8];
            }
#else
            //iOS8之前注册push方法
            //注册Push服务，注册后才能收到推送
            [self registerPush];
#endif
        }
    };
    [XGPush initForReregister:successCallback];
    
    
    //角标清0
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
    
    
    //注册设备
//    [[XGSetting getInstance] setChannel:@"appstore"];
//    [[XGSetting getInstance] setGameServer:@"巨神峰"];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *deviceToken = [defaults objectForKey:@"deviceToken"];
    
    
    NSString *str = [deviceToken stringByReplacingOccurrencesOfString :@" " withString:@""];
    NSLog(@"找到设备码：%@",str);
    [XGPush registerDeviceStr:str];
    
    
    
    /*
    [self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexWidget.onRemoteNotification =function (data){uexXG.cbXgPush(100,0,data);}"];
    */
    [self.meBrwView stringByEvaluatingJavaScriptFromString:@"function gfsdgfsgwrhrwehbwnebvwrebXGMSG(){uexWidget.cbGetPushInfo=function(opId,dataType,data){uexXG.cbXgPush(100,0,data);};setInterval(\'uexWidget.getPushInfo();\',1500);"];
    [self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexWidget.setPushNotifyCallback(\'gfsdgfsgwrhrwehbwnebvwrebXGMSG\');"];
    [self jsSuccessWithName:@"uexXG.cbXgPush" opId:1 dataType:0 strData:str];
}



//绑定信鸽的标签。
-(void)SetXgTag:(NSMutableArray *)array{
     if ([array isKindOfClass:[NSMutableArray class]] && [array count]>0) {
     }
     NSLog(@"in SetXgTag");
     
     NSString *tag = [array objectAtIndex:0];
     
     
     [XGPush setTag:tag];
}

-(void)SetInstallChannel:(NSMutableArray *)array{
     NSLog(@"in SetInstallChannel");
     //[XGPush setTag:[array objectAtIndex:0] successCallback:nil errorCallback:nil];
     //[self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexXG.cbXgPush(0,0,'SetInstallChannel');"];
}


-(void)DelXgTag:(NSMutableArray *)array{
     NSLog(@"in DelXgTag");
     
     [XGPush delTag:[array objectAtIndex:0] successCallback:nil errorCallback:nil];
     //[self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexXG.cbXgPush(0,0,'DelXgTag');"];
}

//当前窗口调用uexWindow.close()接口的时候 插件的clean方法会被调用
-(void)clean{
     
}


-(void)registerPushForIOS8{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
    
    //Types
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    
    //Actions
    UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
    
    acceptAction.identifier = @"ACCEPT_IDENTIFIER";
    acceptAction.title = @"Accept";
    
    acceptAction.activationMode = UIUserNotificationActivationModeForeground;
    acceptAction.destructive = NO;
    acceptAction.authenticationRequired = NO;
    
    //Categories
    UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
    
    inviteCategory.identifier = @"INVITE_CATEGORY";
    
    [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];
    
    [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal];
    
    NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
    
    
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    
    
    [[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
}

- (void)registerPush{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}




@end
